<?php
class Common_Doctrine_Hydrator_NavigationArray extends Doctrine_Hydrator_Abstract
{
    public function hydrateResultSet($stmt)
    {
        $data = $stmt->fetchAll(PDO::FETCH_NUM);
        $className = Zend_Registry::get('navigation_class');
        $arrResult = array();
        if (count($data)) {
            $arrTree = array();
            foreach ($data as $arrLine) {
                if ($arrLine[1] == 0) {
                    $arrTree[$arrLine[0]]['label'] = $arrLine[3];
                    $arrTree[$arrLine[0]]['uri'] = $arrLine[4];
                    $arrTree[$arrLine[0]]['class'] = $className;
                }
            }
            foreach ($data as $arrLine) {
                if ($arrLine[1] != 0) {
                    $arrTree[$arrLine[1]]['pages'][$arrLine[0]]['label'] = $arrLine[3];
                    $arrTree[$arrLine[1]]['pages'][$arrLine[0]]['uri'] = $arrLine[4];
                }
            }
            foreach ($arrTree as $arrItems) {
                $arrSubResult = array(
                    'label' => $arrItems['label'],
                    'uri' => $arrItems['uri'],
                    'class' => $arrItems['class'],
                );
                if (isset($arrItems['pages'])) {
                    foreach ($arrItems['pages'] as $arrPage) {
                        $arrSubResult['pages'][] = array(
                            'label' => $arrPage['label'],
                            'uri' => $arrPage['uri'],
                        );
                    }
                }
                $arrResult[] = $arrSubResult;
            }
        }
        return $arrResult;
    }
}
?>